<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generate cryptographically secure randomness</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.random-engine-secure.html">« Random\Engine\Secure</a></li>
      <li style="float: right;"><a href="class.random-randomerror.html">Random\RandomError »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.random-engine-secure.html">Random\Engine\Secure</a></li>
    <li>Generate cryptographically secure randomness</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="random-engine-secure.generate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Random\Engine\Secure::generate</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.2.0)</p><p class="refpurpose"><span class="refname">Random\Engine\Secure::generate</span> &mdash; <span class="dc-title">Generate cryptographically secure randomness</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-random-engine-secure.generate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Random\Engine\Secure::generate</strong></span>(): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Returns cryptographically secure randomness.
  </p>

  
<p class="para">
 此函数使用的随机性来源如下：
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <span class="simpara">
   在 Windows 上始终使用 <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx" class="link external">&raquo;&nbsp;<span class="function"><strong>CryptGenRandom()</strong></span></a>。在
   PHP 7.2.0 起，将始终使用
   <a href="https://docs.microsoft.com/en-us/windows/desktop/SecCNG/cng-portal" class="link external">&raquo;&nbsp;CNG-API</a> 代替。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   在 Linux 上，如果有效，将使用
   <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom(2)</a>
   系统调用。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   其它平台将使用 <var class="filename">/dev/urandom</var>。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   如果上述来源均不可用，然后会抛出 <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>。
  </span>
 </li>
</ul>

 </div>


 <div class="refsect1 parameters" id="refsect1-random-engine-secure.generate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-random-engine-secure.generate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A string containing <strong><code>PHP_INT_SIZE</code></strong> cryptographically secure random bytes.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-random-engine-secure.generate-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   
 <li class="listitem">
  <span class="simpara">
   如果没有找到合适的随机性来源，将会抛出 <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>。
  </span>
 </li>

  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-random-engine-secure.generate-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-3507">
   <p><strong>示例 #1 <span class="function"><strong>Random\Engine\Secure::generate()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
...
</pre></div>
   </div>
  </div>
 </div>



</div></div></div></body></html>